<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Advance_payment extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}

	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array('account' => $this->advance_payment_model->get_cust_all2()));
	  $this->load->view('layouts/footer');
	 }

	public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array('account' => $this->advance_payment_model->get_cust_all(),
																   'paytype' => $this->advance_payment_model->get_paytype('PAYTYPE')));
	}

	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array('datus'	=> $this->advance_payment_model->get_status($id),
																	'datus2'=> $this->advance_payment_model->get_prepayments($id),
																	'account' => $this->advance_payment_model->get_cust_all(),
																    'paytype' => $this->advance_payment_model->get_paytype('PAYTYPE')));
	}

	public function view_form($id){
		$this->load->view($this->session->userdata('forview'));
	}

	public function get_status() {
		$account_id = $this->input->post('account_id');
		$tmp = ''; $count = 0;
		$res = $this->advance_payment_model->get_status($account_id);
		foreach ($res as $r) {
			$tmp .= "<tr><input id='boang' type='hidden' value=".$r->account_id." name='gee'/><td>".$r->acct_name."</td><td align='right'>".$r->amount.'</td><td align="center"><button type="button" class="btn btn-outline btn-sm btn-primary btn-xs edit-tab" data-toggle="tab" role="tab" href="#view" title="Edit"><i class="fa fa-file-text-o fa-lg"></i></button></td></td></tr>';
			$count++;
		}
		echo json_encode(array('result' => $tmp,'count' => $count++));
	}

	public function insert(){
		$account_id	=	$this->input->post('account_id');
		$amount	=	$this->input->post('amount');
		$payment_type = $this->input->post('payment_type');
		$ref_no = $this->input->post('ref_no');
		$ref_no = $this->input->post('doc_no');
		$data = array(
				$account_id,
				$amount,
				$this->session->userdata('user_id'),
				$payment_type,
				$ref_no,
				$doc_no
			);
		$this->advance_payment_model->insert($data);
	}

	public function update(){
		$account_id	=	$this->input->post('adv_pay_id');
		$amount	=	$this->input->post('new_amount');
		$data = array(
				$account_id,
				$amount,
				$this->session->userdata('user_id')
			);
		$this->advance_payment_model->update($data);
	}

}